/**********************************************************************************
 * Copyright (c) 2012 - 2012,  quick_tools, All right reserved
 * Title: com.adc.quicktools.net  UpOrDownControl.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-6-26 下午2:10:27 
 *********************************************************************************/


package com.adc.quicktools.net;

import java.util.Properties;

import com.adc.quicktools.StringUtil;
import com.adc.quicktools.constants.ConstantDefine;



public class UpOrDownControl {
	private String executeReturnInfo="";
	private Long maxSecond=-1L;
	
	/**
	 * 
	 * Description: 下载或上传统一接口，支持ftp上传下载、samba上传下载、http单线程下载
	 * author:  shen
	 * @param type
	 * @param hostInfo
	 * @param remotePath
	 * @param localPath  
	 * @return int
	 */
	public int upOrDownByType(String type,Properties hostInfo,String remotePath,String localPath) {

		int result=-1;
		ExecuteThread eThread=null;
		if( ((hostInfo!=null && !hostInfo.isEmpty()&&StringUtil.checkStringValue(type)) ||
				(hostInfo==null && ConstantDefine.HTTP_DOWN.equals(type))
				)&&StringUtil.checkStringValue(remotePath) 
				&& StringUtil.checkStringValue(localPath) ){
			try {
				eThread=new ExecuteThread(type, hostInfo, remotePath, localPath);
				eThread.start();
				Long time=0L;
				
				while (time<getMaxSecond() ) {
					if (eThread.getStatus()==0) {
						result=eThread.getStatus();
						break;
					}
					Thread.sleep(ConstantDefine.waitGap);
					System.out.println(" The operation is processing, please wait for a moment！ ");
					time=time+ConstantDefine.waitGap;
					if (!eThread.isAlive()) {
						result=eThread.getStatus();
						break;
					}
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				executeReturnInfo=eThread.getReturnInfo();
				eThread=null;
				System.gc();
			}
		}
		return result;
	}

	/**
	 * 
	 * Description:  获取执行过程中的回执信息！
	 * author:  shen  
	 * @return String
	 */
	public String getExecuteReturnInfo() {
		return executeReturnInfo;
	}

	public Long getMaxSecond() {
		if (maxSecond<0) {
			maxSecond=ConstantDefine.MaxWaitTime;
		}
		return maxSecond;
	}

	public void setMaxSecond(Long maxSecond) {
		this.maxSecond = maxSecond;
	}


}
